import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import Login from "../views/Login";
import Welcome from "../views/Welcome";
import UserList from "../views/user/UserList";
import RoleList from "../views/right/RoleList";
import AuthList from "../views/right/AuthList";


import {nextDate} from "element-ui";

Vue.use(VueRouter)

const routes = [
    {
        path: '/',
        redirect: '/login'
    },
    {
        path: '/login',
        name: 'Login',
        meta: {
            title: '曼巴商城管理系统'
        },
        component: Login
    },
    {
        path: '/home',
        name: 'Home',
        component: Home,
        meta: {
            title: '曼巴商城管理系统'
        },
        redirect: '/welcome',
        children: [
            {
                path: '/welcome',
                name: 'Welcome',
                component: Welcome
            },
            {
                path: 'userlist',
                name: 'UserList',
                component: UserList
            },
            {
                path: 'authlist',
                name: 'AuthList',
                component: AuthList
            },
            {
                path: 'rolelist',
                name: 'RoleList',
                component: RoleList
            }
        ]
    }
]

const router = new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes
})

//导航守卫
router.beforeEach((to, from, next) => {
    document.title = to.matched[0].meta.title
    /*如果是第一次请求登录，就放行*/
    if (to.path == '/login') return next();
    const tokenStr = window.sessionStorage.getItem("token")
    /*如果本地没有token存储，则强制跳转到登录页*/
    if (!tokenStr) return next("/login")
    next()
})

export default router
